/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.persistence.hibernate.criteria.order;

import java.util.Map;

import com.genia.toolbox.basics.manager.CollectionManager;
import com.genia.toolbox.persistence.criteria.order.Order;
import com.genia.toolbox.persistence.hibernate.bean.Queryable;
import com.genia.toolbox.persistence.hibernate.bean.impl.QueryableDisplayObject;
import com.genia.toolbox.persistence.hibernate.criteria.HibernateCriteria;
import com.genia.toolbox.persistence.hibernate.criteria.field.HibernateField;

/**
 * the base of the implementation of <code>Order</code> for the hibernate
 * implementation of the persistence API.
 */
public class HibernateOrder
    implements Order, Queryable
{

  /**
   * Wether this order is ascendent of descendent.
   */
  public enum Direction
  {
    /**
     * constant for the ascendent order.
     */
    ASC("ASC"),
    /**
     * constant for the descendent order.
     */
    DESC("DESC");

    /**
     * Constructor.
     * 
     * @param direction
     *          the parameter needed by hibernate to determine if the order is
     *          ascendent or descendent
     */
    Direction(final String direction)
    {
      this.direction = direction;
    }

    /**
     * the parameter needed by hibernate to determine if the order is ascendent
     * or descendent.
     */
    private String direction;



    /**
     * getter for the direction property.
     * 
     * @return the direction
     */
    public String getDirection()
    {
      return direction;
    }

  }

  /**
   * The field we order with.
   */
  private final transient HibernateField field;

  /**
   * The direction of this sort.
   */
  private final transient Direction direction;



  /**
   * Constructor.
   * 
   * @param direction
   *          the direction of this sort
   * @param field
   *          the field we order with
   */
  public HibernateOrder(final Direction direction, final HibernateField field)
  {
    this.direction = direction;
    this.field = field;
  }



  /**
   * append to stringBuilder the part of the query represented by this object.
   * 
   * @param canHaveParameter
   *          can this part of the query use named parameters?
   * @param collectionManager
   *          The <code>CollectionManager</code> that allow us to perform
   *          joins
   * @param criteria
   *          the <code>HibernateCriteria</code> the represents the all query
   * @param stringBuilder
   *          the <code>StringBuilder</code> to append to.
   * @param queryableDisplayObject
   *          the <code>DisplayObject</code> that allows to convert this
   *          <code>Queryable</code> to a <code>String</code>
   */
  public void appendQuery(final boolean canHaveParameter, final CollectionManager collectionManager, final HibernateCriteria<?> criteria, final StringBuilder stringBuilder,
      final QueryableDisplayObject queryableDisplayObject)
  {
    field.appendQuery(canHaveParameter, collectionManager, criteria, stringBuilder, queryableDisplayObject);
    stringBuilder.append(" ");
    stringBuilder.append(direction.getDirection());
  }



  /**
   * Fill the parameters <code>Map</code> with the constants elements of this
   * query.
   * 
   * @param parameters
   *          the <code>Map</code> to be filled
   */
  public void fillParameters(final Map<String, Object> parameters)
  {
    field.fillParameters(parameters);
  }

}
